package com.xdf.docmanagementsystem.service.impl;

import com.xdf.docmanagementsystem.common.base.BaseService;
import com.xdf.docmanagementsystem.common.util.CodeGenerateUtil;
import com.xdf.docmanagementsystem.enums.ResponseEnum;
import com.xdf.docmanagementsystem.service.DocDepartmentService;
import com.xdf.docmanagementsystem.service.DocExtraFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xdf.docmanagementsystem.entity.Document;
import java.util.*;
import com.xdf.docmanagementsystem.dao.DocumentMapper;
import com.xdf.docmanagementsystem.service.DocumentService;
import org.springframework.util.Assert;

/**
 * @Description : com.xdf.docmanagementsystem.service.impl(业务层实现类,该类为生成器自动生成).
 * @Author : ZK
 * @Date : 2020-06-21 12:45:28
 */
@Service
public class DocumentServiceImpl extends BaseService implements DocumentService {
    @Autowired
    private DocumentMapper dao;
    @Autowired
    private DocExtraFileService service;
    @Autowired
    private CodeGenerateUtil codeGenerateUtil;
    @Autowired
    private DocDepartmentService departmentService;

    /**
     * @Describe 新增方法
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(Document record) throws Exception {
        return dao.insert(record);
    }

    /**
     * @Describe 选择新增
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(Document record) throws Exception {
        Assert.notNull(record, ResponseEnum.NOT_ALLOW.getMessage());
        Assert.notNull(record.getDocName(), ResponseEnum.NOT_ALLOW.getMessage());
        Assert.notNull(record.getDocDeptId(), "未选中档案种类!");
        record.setDocNumber(codeGenerateUtil.generateDocumentCode());
        record.setCreateUserId(getUserId());
       /* List<Document> list = dao.selectByParam(
                Document.builder()
                        .docName(record.getDocName())
                        .build());
        Assert.state(list != null && list.size() <= 0, ResponseEnum.NAME_DUMP.getMessage());*/
        return dao.insertSelective(record);
    }

    /**
     * @Describe 批量新增
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiInsert(List<Document> list) throws Exception {
        return dao.multiInsert(list);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(Document record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 选择修改
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(Document record) throws Exception {
        return dao.updateByPrimaryKeySelective(record);
    }

    /**
     * @Describe 修改
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Document record) throws Exception {
        return dao.updateByPrimaryKey(record);
    }

    /**
     * @Describe 批量修改
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int multiUpdate(List<Document> list) throws Exception {
        return dao.multiUpdate(list);
    }

    /**
     * @Describe 根据主键查询
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    public Document selectByPrimaryKey(Integer id) throws Exception {
        return dao.selectByPrimaryKey(id);
    }

    /**
     * @Describe 根据主键删除
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByPrimaryKey(Integer id) throws Exception {
        return dao.deleteByPrimaryKey(id);
    }

    /**
     * @Describe 根据Entity查询
     * @author ZK
     * @Date 2020-06-21 12:45:28
     */
    @Override
    public List<Document> listQuery(Document record) throws Exception {
        return dao.selectByParam(record);
    }

    /**
     * 查询
     *
     * @param departmentId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> report(Integer departmentId, Integer status) throws Exception {
        List<Integer> list = departmentService.findChildrenId(departmentId);
        if (list != null && list.size() > 0) {
            return dao.report(list.stream().toArray(Integer[]::new), status);
        } else {
            return dao.report(null, status);
        }
    }

}